package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.CommodityClassification;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.dao.mapper.CommodityClassificationMapper;
import club.xyes.zkh.ecommerce.service.CommodityClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Create by 郭文梁 2019/4/17 0017 10:06
 * CommodityClassificationServiceImpl
 * 商铺分类相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@Service
public class CommodityClassificationServiceImpl extends BaseServiceImpl<CommodityClassification> implements CommodityClassificationService {
    private final CommodityClassificationMapper mapper;

    @Autowired
    public CommodityClassificationServiceImpl(CommodityClassificationMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public List<CommodityClassification> findByUserId(Integer userId) {
        return mapper.selectByUserId(userId);
    }

    @Override
    public List<CommodityClassification> findByShop(Shop shop) {
        CommodityClassification query = new CommodityClassification();
        query.setShopId(shop.getId());
        return findByQuery(query);
    }

    @Override
    public Map<String, Long> countCommodityByShop(Shop shop) {
        List<Map<String, Object>> res = mapper.selectCommodityCountByShopId(shop.getId());
        return res.stream().collect(Collectors.toMap(v -> (String) v.get("name"), v -> (Long) v.get("count")));
    }
}
